<?php
namespace app\common;

class Pkcs7Encoder
{

    private const BlockSize = 32;

    public static function encode($src)
    {
        $count = strlen($src);
        $amountToPad = self::BlockSize - ($count % self::BlockSize);
        $pad = chr($amountToPad);
        $pads = str_repeat($pad, $amountToPad);
        $length = $count + $amountToPad;
        $dest = $src . $pads;
        return $dest;
    }

    public static function decode($decrypted)
    {
        $pad = ord($decrypted[strlen($decrypted) - 1]);
        if ($pad < 1 || $pad > self::BlockSize) {
            $pad = 0;
        }
        if ($pad > 0) {
            return substr($decrypted, 0, strlen($decrypted) - $pad);
        }
        return $decrypted;
    }
}
?>
